package com.czy.owner.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.czy.owner.R;
import com.hyphenate.chat.EMGroup;

import java.util.List;


public class MessageGroupAdapter extends BaseRecyclerAdapter<EMGroup, MessageGroupAdapter.MessageGroupHolder> {

    public MessageGroupAdapter(Context context, List<EMGroup> list ) {
        super(context, list);
    }

    @Override
    public MessageGroupHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_message_group_recycler, parent, false);
        return new MessageGroupHolder(view);
    }

    @Override
    public void onBindViewHolder(final MessageGroupHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
        final View finalView = holder.itemView;



        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EMGroup mg = data.get(position);
                data.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, data.size() - position);
                if(onItemDeleteListener!=null){
                    onItemDeleteListener.onItemDelete(position, mg, v);
                }
            }
        });

    }

    @Override
    public void onViewRecycled(MessageGroupHolder holder) {
        super.onViewRecycled(holder);
    }

    public class MessageGroupHolder extends BaseRecyclerAdapter.ViewHolder {
        private ImageView ivAvatar;
        private TextView tvTitle;
        private TextView tvDelete;
        public MessageGroupHolder(View view) {
            super(view);
            ivAvatar = (ImageView)view.findViewById(R.id.iv_message_group_item_avatar);
            tvTitle = (TextView) view.findViewById(R.id.tv_message_group_item_title);
            tvDelete = (TextView) view .findViewById(R.id.tv_item_delete);
        }

        public void bindData(EMGroup mMessageGroup) {
            tvTitle.setText(mMessageGroup.getGroupName());
            /*if(!mMessageGroup.getAvatar().isEmpty()){
                ivAvatar.setTag(mMessageGroup.getAvatar());
                x.image().bind(ivAvatar, mMessageGroup.getAvatar(), new Callback.CommonCallback<Drawable>() {
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onSuccess(Drawable result) {
//                        CircleImageViewTools.toCircleImageView(ivAvatar);
                    }
                });
            }else{
                ivAvatar.setImageResource(R.mipmap.icon_message_group_none);
            }*/
        }

    }


}
